﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class employer_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cboFoodID.SelectedIndexChanged += new EventHandler(cboFoodID_SelectedIndexChanged);
        btCapnhat_mon.Click += new EventHandler(btCapnhat_mon_Click);

        if (ck_undelete.Checked == false)
        {
            ck_delete.Checked = true;
        }

        DBconnect connect = new DBconnect();
        connect.Open();

        string sql_IDmonan = "Select * from FastFood";
        if (connect.IsConnected == true)
        {
            if (!IsPostBack)
            {
                DataTable DBTable_FastFood = new DataTable();
                DBTable_FastFood = connect.GetDataTable(sql_IDmonan);
                cboFoodID.DataSource = DBTable_FastFood;
                cboFoodID.DataTextField = "Name";
                cboFoodID.DataValueField = "FoodId";
                cboFoodID.DataBind();
                cboFoodID_SelectedIndexChanged(null, null);
            }
        }
        connect.Close();
    }

    void btCapnhat_mon_Click(object sender, EventArgs e)
    {
        DBconnect connection = new DBconnect();
        connection.Open();
        if (connection.IsConnected == true)
        {
            int isdelete;
            if (ck_delete.Checked == true)
            {
                isdelete = 1;
            }
            else
            {
                isdelete = 0;
            }
            int ID = Int32.Parse(cboFoodID.SelectedValue);
            string[] listcol = new string[] { "IsDelete" };
            object[] listobject = new object[] { isdelete };
            int n = connection.Update("FastFood", listcol, listobject, "FoodId=@ID", "@ID", ID);
            if (n > 0)
                lbcnmon_error.Text = "Đã cập nhật";
            else
                lbcnmon_error.Text = "Lỗi cập nhật";
        }
        connection.Close();
    }

    void cboFoodID_SelectedIndexChanged(object sender, EventArgs e)
    {

        lbcnmon_error.Text = "";
        DBconnect con = new DBconnect();
        con.Open();

        string sql_monan = "Select ff.Name as Food_Name, ff.Unit as Food_unit, ff.Cost as Food_cost, ft.Name as FT_Name, lb.Name as LB_Name, '<img width=\"103pt\" height= \"115pt\" src=../images/Foods/\"' + ff.Image + '\" />' as ImgUrl  from FastFood ff, FoodType ft, Label lb ";
        sql_monan += "where FoodId='" + cboFoodID.SelectedValue + "'";
        sql_monan += "and ff.FoodTypeId=ft.FoodTypeId and ff.LabelId=lb.LabelId";

        string noidung = "";
        if (con.IsConnected == true)
        {
            //lbcnmon_error.Text += "connected....";
            //if (!IsPostBack)
            //{
            DataTable DBTable_FastFood_Detail = new DataTable();
            DBTable_FastFood_Detail = con.GetDataTable(sql_monan);
            if (DBTable_FastFood_Detail.Rows.Count > 0)
            {

                noidung += "<div class=\"fm-mon\">";
                noidung += DBTable_FastFood_Detail.Rows[0]["ImgUrl"];
                noidung += "</div>";

                noidung += "<div class=\"fm-mon\">";
                noidung += "<label>Tên món ăn: </label>";
                noidung += "<label>" + DBTable_FastFood_Detail.Rows[0]["Food_Name"] + "</label>";
                noidung += "</div>";

                noidung += "<div class=\"fm-mon\">";
                noidung += "<label>Loại món ăn: </label>";
                noidung += "<label>" + DBTable_FastFood_Detail.Rows[0]["FT_Name"] + "</label>";
                noidung += "</div>";

                noidung += "<div class=\"fm-mon\">";
                noidung += "<label>Giá: </label>";
                noidung += "<label>" + DBTable_FastFood_Detail.Rows[0]["Food_cost"] + "       " + DBTable_FastFood_Detail.Rows[0]["Food_unit"] + "</label>";
                noidung += "</div>";


                noidung += "<div class=\"fm-mon\">";
                noidung += "<label>Giới thiệu món ăn: </label>";
                noidung += "<asp:TextBox ID=\"txtchitiet\" runat=\"server\" Height=\"135px\" TextMode=\"MultiLine\"></asp:TextBox>";
                //noidung += "txtchitietgioithieumonan.text=" + DBTable_FastFood_Detail.Rows[0]["LB_Name"];                    
                noidung += "</div>";

                //lbcnmon_error.Text = "Read successful..";
            }
            //else
                //lbcnmon_error.Text = "Error when query...";
            //}
        }
        else
        {
            lbcnmon_error.Text += "Lỗi kết nối dữ liệu";
        }
        this.noidungchitietmon.InnerHtml = noidung;
        con.Close();
    }
    protected void ck_delete_CheckedChanged(object sender, EventArgs e)
    {
        ck_delete.Checked = true;
        ck_undelete.Checked = false;
        lbcnmon_error.Text = "";
    }
    protected void ck_undelete_CheckedChanged(object sender, EventArgs e)
    {
        ck_undelete.Checked = true;
        ck_delete.Checked = false;
        lbcnmon_error.Text = "";
    }
}
